package com.erb.controller.zwj;


import com.erb.entity.InformVo;
import com.erb.entity.LoanUser;
import com.erb.service.hzg.LoanUserService;
import com.erb.service.zwj.IInformService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inform")
public class InformController {
	@Resource
	private IInformService informService;
	@Resource
	private LoanUserService loanUserService;

	private DateFormat dateFormat = DateFormat.getDateTimeInstance();

	@GetMapping("/findInforms")
	public ModelAndView findInforms(HttpSession session) {
		ModelAndView modelAndView = new ModelAndView("loan/inform/list");
		LoanUser loanUserBySession = (LoanUser) session.getAttribute("loanuser");

		System.out.println(loanUserBySession.getId());
		List<InformVo> informList = informService.findInformsByRecipientsIdForLoan(loanUserBySession.getId());
		Map<Integer, String> dateMap = new HashMap<>(informList.size());
		for (InformVo informVo : informList) {
			if (informVo.getInformSenderName() == null && informVo.getInformSenderId() != null && !informVo.getInformSenderId().equals("0")) {
				LoanUser loanUser = loanUserService.lulist(Integer.parseInt(informVo.getInformSenderId()));
				if (loanUser != null) {
					informVo.setInformSenderName(loanUser.getName());
				}
			}
			dateMap.put(informVo.getInformId(), dateFormat.format(new Date(informVo.getSendTime())));
		}

		modelAndView.addObject("informList", informList);
		modelAndView.addObject("dateMap", dateMap);
		return modelAndView;
	}
}
